﻿//
//  Copyright (c) 2008, WebAula S/A 
//  All rights reserved.
//
//  Authors : 
//
//           * Ivan Paulovich (ivan@100loop.com)
//           Blog: http://www.100loop.com/          
//           Messenger: ivanpaulovich@hotmail.com 
//           Twitter: @ivanpaulovich
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Domain.Model;
using NHibernate;
using NHibernate.Criterion;

namespace Core.Domain.Repositories
{
    public class CommentRepository : IRepository<Comment>
    {
        #region IRepository<Comment> Members

        void IRepository<Comment>.Save(Comment entity)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Save(entity);
                    transaction.Commit();
                }
            }
        }

        void IRepository<Comment>.Update(Comment entity)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Update(entity);
                    transaction.Commit();
                }
            }
        }

        void IRepository<Comment>.Delete(Comment entity)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Delete(entity);
                    transaction.Commit();
                }
            }
        }

        Comment IRepository<Comment>.GetById(int id)
        {
            using (ISession session = NHibernateHelper.OpenSession())
                return session.CreateCriteria<Comment>().Add(Restrictions.Eq("CommentId", id)).UniqueResult<Comment>();
        }

        IList<Comment> IRepository<Comment>.GetAll()
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                ICriteria criteria = session.CreateCriteria(typeof(Comment));
                return criteria.List<Comment>();
            }
        }

        #endregion
    }
}
